import { Injectable } from '@nestjs/common';
import { getCurrentTime } from '../../utils';
import { Response } from '../../utils/response';
import { TiktokRepository } from './tiktok.repository';

@Injectable()
export class TiktokService {
  constructor(private readonly tiktokRepository: TiktokRepository) {}

  //   查询tiktok列表
  async queryTiktokListService() {
    const tiktokList = await this.tiktokRepository.queryTiktokListRepository();
    if (Array.isArray(tiktokList)) {
      return Response.success({
        data: tiktokList,
      });
    } else {
      return Response.error({
        message: '查询失败',
        data: tiktokList,
      });
    }
  }

  //   新增
  async addTiktokService(tiktokDto: any) {
    tiktokDto = {
      ...tiktokDto,
      delFlag: 0,
      createdAt: getCurrentTime('YYYY-MM-DD HH:mm:ss'),
      updatedAt: getCurrentTime('YYYY-MM-DD HH:mm:ss'),
    };
    const tiktok = await this.tiktokRepository.addTiktokRepository(tiktokDto);
    if (tiktok) {
      return Response.success({
        message: '新增成功',
        data: tiktok,
      });
    } else {
      return Response.error({
        message: '新增失败',
        data: tiktok,
      });
    }
  }

  //   删除
  async deleteTiktokService(id: number) {
    if (!id) {
      return Response.error({
        message: 'id is not exist',
      });
    }
    const result = await this.tiktokRepository.deleteTiktokRepository(id);
    if (result.affected === 1) {
      return Response.success({
        message: '删除成功',
      });
    } else {
      return Response.error({
        message: '删除失败',
      });
    }
  }

  //   修改
  async updateTiktokService(id: number, tiktokDto: any) {
    if (!id) {
      return Response.error({
        message: 'id不能为空',
      });
    }
    tiktokDto = {
      ...tiktokDto,
      updatedAt: getCurrentTime('YYYY-MM-DD HH:mm:ss'),
    };
    const result = await this.tiktokRepository.updateTiktokRepository(
      id,
      tiktokDto,
    );
    if (result.affected === 1) {
      return Response.success({
        message: '修改成功',
      });
    } else {
      return Response.error({
        message: '修改失败',
      });
    }
  }
}
